/*
 * @Author: ipk518 18163976442@163.com
 * @Date: 2023-08-15 11:36:30
 * @LastEditors: ipk518 121206530@qq.com
 * @LastEditTime: 2024-09-26 16:59:14
 * @FilePath: /WingsUpdater-main/src/PackageResolver/PackageResolverUdp.cpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

#include "PackageResolverUdp.h"
#include "IDataTransmission.h"
#include "WingsTcpUdpProto.h"
#include "myLog.h"


using namespace std;

PackageResolverUdp::PackageResolverUdp() 
    :m_udpHandle(IDataTransmission::create(DATA_TRANSMISSION_UDP))
{
    m_type = "udp";
}

Result PackageResolverUdp::init() 
{
    qlog_d("package resolver(%s) init.", m_type.c_str());

    return m_udpHandle->init();
}

Result PackageResolverUdp::resolve(string &aliasReceiver, vector<uint8_t> &dataReceiver)
{
    if (false == m_udpHandle->wait(-1)) 
    {
        return Result::create(ErrorKind::Timeout);
    }

    // recv
    auto udpPackage = reinterpret_cast<WingsInternalWrapper *>(m_udpHandle->take());

    aliasReceiver = std::move(udpPackage->name);
    dataReceiver = std::move(udpPackage->byteArray);
    m_udpHandle->release(udpPackage);
    udpPackage = nullptr;

    return Result();
}

Result PackageResolverUdp::respond(const bool success, const std::string &detail) 
{
    return m_udpHandle->transmit(detail.data(), detail.size());
}